package com.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manager.mapper.TbUserMapper;
import com.manager.pojo.TbUser;
import com.manager.pojo.TbUserExample;
import com.manager.pojo.TbUserExample.Criteria;
import com.manager.service.UserService;
import com.manager.vo.TbUserEx;

/**
 * 
 * @Title:  UserServiceimpl.java     
 * @Description:    账号信息接口实现类Service
 * @Company: www.yangshuai.com
 * @author:      
 * @date:   2019年2月14日 下午3:47:15   
 * @version V1.0
 */
@Service
public class UserServiceimpl implements UserService {

	@Autowired
	private TbUserMapper tbUserMapper;
	
	@Override		//获取账号分页信息
	public TbUserEx getUserListInfo(Integer page, Integer rows) throws Exception {

		TbUserEx tbUserEx = new TbUserEx();
		List<TbUser> list = null;
		
		TbUserExample example = new TbUserExample();
		example.setOrderByClause("id desc");
		//设置分页范围
		PageHelper.startPage(page,rows);
		
		try {
			list = tbUserMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("账号信息获取异常");
			
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		
		return tbUserEx;
	}

	@Override		//添加账号
	public TbUserEx addUser(TbUser tbUser) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbUser>list = null;
		TbUserExample example = new TbUserExample();
		Integer result = 0;
		
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(tbUser.getUsername());
		
		try {
			list = tbUserMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		if(list.size()>0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("用户名已存在");
			return tbUserEx;
		}
		
		
		try {
			result = tbUserMapper.insertSelective(tbUser);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		if(result<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("账号添加失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("账号添加成功");
		
		return tbUserEx;
	}

	@Override		//根据ID删除账号
	public TbUserEx delUser(Integer id) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		
		try {
			result = tbUserMapper.deleteByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("账号删除异常");
			return tbUserEx;
		}
		
		if(result<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("账号删除失败");
			return tbUserEx;
		}
		
		return tbUserEx;
	}

	
	@Override		//编辑账号信息
	public TbUserEx editUser(TbUser tbUser) {

		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		
		try {
			result = tbUserMapper.updateByPrimaryKey(tbUser);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("账号编辑异常");
			return tbUserEx;
		}
		
		if(result<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("账号编辑失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("用户编辑成功");
		return tbUserEx;

	}

	
	@Override		//根据ID获取账号
	public TbUser getUserById(Integer id) throws Exception {

		TbUser tbUser = new TbUser();
	
		try {
			tbUser = tbUserMapper.selectByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return tbUser;
	}

	
	@Override
	public TbUserEx getUserByUsernameAndPassword(TbUser tbUser) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbUser> list = null;
		TbUserExample example = new TbUserExample();
		
		Criteria criter_username = example.createCriteria();
		criter_username.andUsernameEqualTo(tbUser.getUsername());
		
		try {
			list = tbUserMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			 throw new Exception();
		}
		if(list.size()>0) {
			
			example = new TbUserExample();
			Criteria criter_pass = example.createCriteria();
			criter_pass.andUsernameEqualTo(tbUser.getUsername());
			criter_pass.andPasswordEqualTo(tbUser.getPassword());
			criter_pass.andPurviewEqualTo(tbUser.getPurview());
			list = tbUserMapper.selectByExample(example);
			
			if(list.size()<=0 || list == null) {
				tbUserEx.setCode(400);
				tbUserEx.setError("密码错误");
				return tbUserEx;
			}
			
		}else{
			tbUserEx.setCode(400);
			tbUserEx.setError("用户名不存在");
			return tbUserEx;
		}
		
		tbUserEx.setRows(list);
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("登入成功");
		
		return tbUserEx;
	}

}
